package com.sl.mes.app.controller.mm;

import com.fasterxml.jackson.databind.JsonNode;
import com.sl.mes.app.dao.mm.AggBgVo;
import com.sl.mes.app.service.mm.MmProconWrService;
import com.sl.mes.app.service.puboid.PubOidService;
import com.sl.mes.app.util.JsonHandler;
import com.sl.mes.core.util.JacksonUtil;
import com.sl.mes.core.util.ResponseUtil;
import com.sl.mes.db.dao.BdWkMapper;
import com.sl.mes.db.dao.MmMoMapper;
import com.sl.mes.db.itf.bd.IBdWk;
import com.sl.mes.db.itf.mm.IMmMo;
import com.sl.mes.db.vo.BdWk;
import com.sl.mes.db.vo.MmMo;
import com.sl.mes.db.vo.MmPmo;
import com.sl.mes.db.vo.MmProconWr;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/mes/mmproconwr")
@Validated
public class MmProconWrController {

    @Resource
    private MmProconWrService mmProconWrService;

    @Resource
    private PubOidService pubOidService;

    @Resource
    private IBdWk iBdWk;

    @Resource
    private IMmMo iMmMo;

    @Resource
    private MmMoMapper mmMoMapper;

    @Resource
    private BdWkMapper bdWkMapper;

    /**
     * 根据pk查询报工
     * @param body
     * @param request
     * @return
     */
    @GetMapping("getMmProconWrVoById")
    public Object getAggMoVoById(@RequestBody String body, HttpServletRequest request) {
        String cid = JacksonUtil.parseString(body, "cid");
        Map<Object, Object> result = new HashMap<Object, Object>();
        MmProconWr mmProconWrVo = mmProconWrService.getMmProconWrVoById(cid);
        result.put("mmproconwr",mmProconWrVo);
        return ResponseUtil.ok(result);
    }

    /**
     * 生成报工表
     * @param body
     * @param request
     * @return
     */
    @PostMapping("addMmProconWrVoById")
    public Object addAggMoVoById(@RequestBody String body, HttpServletRequest request) {
        JsonNode movo = (JsonNode) JacksonUtil.toNode(body);
        JsonNode moHead = (JsonNode) ((JsonNode) movo.get("data")).get("AggBgVo");
        AggBgVo aggBgVo = JsonHandler.fromJson(moHead.toString(), AggBgVo.class);


        String vwkcode  = aggBgVo.getBdWkVwkcode();
        BdWk bdWk = iBdWk.findByVwkcode(vwkcode);

        String cid = pubOidService.generate();
        String cwkid =bdWk.getCwkid();

        MmProconWr mmProconWr = new MmProconWr();
        //主键pk
        mmProconWr.setCid(cid);
        //根据主键查询工作中心
        MmPmo mmPmo =iMmMo.findParentVoById(bdWk.getVdef1());
        MmMo mmMo  =mmMoMapper.selectByPrimaryKey(bdWk.getVdef2());

        //判断是否重复报工
        if (mmMo.getFbillstatus().equals(4)){
            return ResponseUtil.fail(0,"您已经报工!");
        }

        //集团
        mmProconWr.setPkGroup(mmPmo.getPkGroup());
        //组织
        mmProconWr.setPkOrg(mmPmo.getPkOrg());
        //最新组织
        mmProconWr.setPkOrgV(mmPmo.getPkOrgV());
        //单据号
        mmProconWr.setVbillcode(mmPmo.getVbillcode());
        //生产订单
        mmProconWr.setCmoid(mmPmo.getCpmohid());
        //生产订单号
        mmProconWr.setCmocode(mmPmo.getVbillcode());
        //生产订单明细
        mmProconWr.setCmobid(mmMo.getCmoid());
        //生产订单行号
        mmProconWr.setVmorowno(mmMo.getVrowno());
        //生产部门
        mmProconWr.setCdeptid(mmMo.getCdeptid());
        //产品最新
        mmProconWr.setCmaterialid(mmMo.getCmaterialid());

        //完工数量
        //mmProconWr.setNwrnum();

        //终端编号
        mmProconWr.setPkProconsole(cid);

        //炉台
        BdWk bdWk1 = iBdWk.findByVwkcode(aggBgVo.getBdWkVwkcode());
        mmProconWr.setCwkid(bdWk1.getCwkid());
        //物料编号
        mmProconWr.setCmaterialid(mmMo.getCmaterialid());
        //长度
        mmProconWr.setVbdef1(aggBgVo.getVdef1());
        //重量
        mmProconWr.setVbdef2(aggBgVo.getVdef2());
        //是否完结
        mmProconWr.setVbdef3(aggBgVo.getVdef3());
        //是否断线
        mmProconWr.setVbdef4(aggBgVo.getVdef4());
        //毛棒编号
        mmProconWr.setVbdef5(aggBgVo.getVdef5());
        //发料编号
        mmProconWr.setVbdef6(aggBgVo.getMaterialCode());

        int res =mmProconWrService.addMmProconWr(mmProconWr);

        //进行判断是否完工不需要复投
        if (mmProconWr.getVbdef3().equals("1")){
            bdWk.setVdef1(null);
            bdWk.setVdef2(null);
            int reg = bdWkMapper.updateByPrimaryKey(bdWk);
            if (reg!=1){
                return ResponseUtil.fail(-1,"报工完成!,清空炉台失败");
            }
        }

        //修改状态码
        mmMo.setFbillstatus(4);
        int reg = mmMoMapper.updateByPrimaryKey(mmMo);

        Map<Object, Object> result = new HashMap<Object, Object>();
        result.put("mmProconWr", mmProconWr);
        if (res>0){
            return ResponseUtil.ok(result);
        }else{
            return ResponseUtil.fail();
        }
    }

    /**
     * 扒锅料
      */
    public Object findBoilerByVwkcode(String vwkcode){
        Map<Object,Object> result = new HashMap<>();

        return ResponseUtil.ok(result);
    }

    /**
     * 闷路料
     */
    public Object findBoringByVwkcode(String vwkcode){
        Map<Object,Object> result = new HashMap<>();

        return ResponseUtil.ok(result);
    }

    /**
     * 提肩
     */
    public Object findPointedByVwkcode(String vwkcode){
        Map<Object,Object> result = new HashMap<>();

        return ResponseUtil.ok(result);
    }

    /**
     * 提渣
     */
    public Object findSlagByVwkcode(String vwkcode){
        Map<Object,Object> result = new HashMap<>();

        return ResponseUtil.ok(result);
    }

    /**
     * 锅底余料量
     */
    public Object findSurplusByVwkcode(String vwkcode){
        Map<Object,Object> result = new HashMap<>();

        return ResponseUtil.ok(result);
    }


    }

